package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.ProductLedgerOriginal.ProductLedgerContent;

public class ProductDetailFragment extends Fragment {
  public static final String EXTRA_CONTENT = "com.isunland.managesystem.entity.extra_content";
  private ProductLedgerContent mContent;
  private TextView tvProductName;
  private TextView tvProductType;
  private TextView tvProductKind;
  private TextView tvProductCode;
  private TextView tvSelfcode;
  private TextView tvApartment;
  private TextView tvmainFunctionDesc;
  private TextView tvmfactoryName;
  private TextView tvproperty;
  private TextView tvRemark;
  private TextView tvpurchasePrice;
  private TextView tvsalePrice;

  private TextView tvregStaffName;
  private TextView tvregDate;
  private TextView tvcheckStaffName;
  private TextView tvcheckDate;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    BaseVolleyActivity activity = (BaseVolleyActivity) getActivity();
    ActionBar actionBar;
    if (activity != null) {
      actionBar = activity.getSupportActionBar();
      if (actionBar != null) {
        if (NavUtils.getParentActivityName(activity) != null) {
          actionBar.setDisplayHomeAsUpEnabled(true);
        }
      }
    }
    // 获取传过来的delegateTask
    mContent = (ProductLedgerContent) getArguments().getSerializable(EXTRA_CONTENT);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      default:
        return super.onOptionsItemSelected(item);
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    // 找到布局控件
    View v = inflater.inflate(R.layout.fragment_product_detail, container, false);
    tvProductName = (TextView) v.findViewById(R.id.tv_product_name);
    tvProductType = (TextView) v.findViewById(R.id.tv_product_type);
    tvProductKind = (TextView) v.findViewById(R.id.tv_product_kind);
    tvProductCode = (TextView) v.findViewById(R.id.tv_product_code);
    tvSelfcode = (TextView) v.findViewById(R.id.tv_product_selfcode);
    tvApartment = (TextView) v.findViewById(R.id.tv_apartment);
    tvmainFunctionDesc = (TextView) v.findViewById(R.id.tv_mainFunctionDesc);
    tvmfactoryName = (TextView) v.findViewById(R.id.tv_mfactoryName);
    tvproperty = (TextView) v.findViewById(R.id.tv_property);
    tvRemark = (TextView) v.findViewById(R.id.tv_remark);
    tvpurchasePrice = (TextView) v.findViewById(R.id.tv_purchasePrice);
    tvsalePrice = (TextView) v.findViewById(R.id.tv_salePrice);

    tvregStaffName = (TextView) v.findViewById(R.id.tv_regStaffName);
    tvregDate = (TextView) v.findViewById(R.id.tv_regDate);
    tvcheckStaffName = (TextView) v.findViewById(R.id.tv_checkStaffName);
    tvcheckDate = (TextView) v.findViewById(R.id.tv_checkDate);
    initDate();
    return v;
  }

  private void initDate() {
    tvProductName.setText(mContent.getProductName());
    tvProductType.setText(mContent.getMtype());
    tvProductKind.setText(mContent.getMaterialKindCom());
    tvProductCode.setText(mContent.getMaterialCode());
    tvSelfcode.setText(mContent.getMaterialSelfCode());
    tvApartment.setText(mContent.getMunit());
    tvmainFunctionDesc.setText(mContent.getMainFunctionDesc());
    tvmfactoryName.setText(mContent.getMfactoryName());
    tvproperty.setText(mContent.getMaterialPropName());
    tvRemark.setText(mContent.getRemark());
    tvpurchasePrice.setText(mContent.getPurchasePrice());
    tvsalePrice.setText(mContent.getSalePrice());
    tvregStaffName.setText(mContent.getRegStaffName());
    tvregDate.setText(mContent.getRegDate());
    tvcheckStaffName.setText(mContent.getCheckStaffName());
    tvcheckDate.setText(mContent.getCheckDate());
  }

  public static ProductDetailFragment newInstance(ProductLedgerContent item) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, item);
    ProductDetailFragment fragment = new ProductDetailFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
